package com.woniuxy.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.user.dto.UserScoreDto;
import com.woniuxy.user.entity.UserAccount;
import com.woniuxy.user.entity.UserInfo;
import com.woniuxy.user.entity.UserScore;
import com.woniuxy.user.service.exception.UserException;
import com.woniuxy.user.mapper.UserAccountMapper;
import com.woniuxy.user.mapper.UserInfoMapper;
import com.woniuxy.user.mapper.UserScoreMapper;
import com.woniuxy.user.service.UserScoreService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.user.form.ChangeUserScoreForm;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ...
 * @since 2024年05月22日
 */
@Service
public class UserScoreServiceImpl extends ServiceImpl<UserScoreMapper, UserScore> implements UserScoreService {

    @Resource
    private UserInfoMapper userInfoMapper;

    @Resource
    private UserAccountMapper userAccountMapper;
    /**
     * 用户积分变动
     * @param form 用户积分变得参数
     */
    @Override
    @Transactional
    public void changeUserScore(ChangeUserScoreForm form) {
        //if(form.getIsAdd()!=0&&form.getIsAdd()!=1) throw new UserException(1000,"只能传0增加 1减少");
        UserAccount userAccount = userAccountMapper.selectById(form.getUserId());
        if(userAccount==null) throw new UserException(10002,"用户不存在");
        List<UserScore> userScores = this.list(new QueryWrapper<UserScore>().eq("user_id", form.getUserId()).orderByDesc("change_time"));
        UserScore userScore = userScores.get(0);
        UserScore newUserScore = new UserScore();
        newUserScore.setUserId(form.getUserId());
        newUserScore.setChangeScore(form.getChangeScore());
        newUserScore.setChangeDetails(form.getChangeDetails());
         Integer score=  userScore.getScoreTotal()+form.getChangeScore();
            if(score<=0){
               score=0;
            }
        newUserScore.setScoreTotal(score);
        newUserScore.setChangeTime(LocalDateTime.now());
        this.save(newUserScore);
        UserInfo userInfo = userInfoMapper.selectById(form.getUserId());
        userInfo.setUserIntegral(newUserScore.getScoreTotal());
        userInfoMapper.updateById(userInfo);
    }

    /**
     * 查询所有用户积分详情
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Page<UserScoreDto> pageAllUserScore(Integer pageNum, Integer pageSize) {
        Page<UserScore>page=new Page<>(pageNum,pageSize);
        QueryWrapper<UserScore>wrapper=new QueryWrapper<>();
        wrapper.groupBy("user_id");
        wrapper.select("user_id");
        this.page(page, wrapper);
        List<UserScore> list = page.getRecords();
        List<UserScoreDto>list1=new ArrayList<>();
        for (UserScore userScore : list) {
            UserInfo userInfo = userInfoMapper.selectOne(new QueryWrapper<UserInfo>().eq("user_id", userScore.getUserId()));
            QueryWrapper<UserScore> wrapper1 = new QueryWrapper<UserScore>().eq("user_id", userScore.getUserId()).orderByDesc("change_time");
            List<UserScore> list2 = this.list(wrapper1);
            UserScoreDto dto = new UserScoreDto();
            UserScore score = list2.get(0);
            dto.setUserId(score.getUserId());
            dto.setChangeScore(score.getChangeScore());
            dto.setScoreTotal(score.getScoreTotal());
            dto.setChangeDetails(score.getChangeDetails());
            dto.setChangeTime(score.getChangeTime());
            dto.setNickName(userInfo.getNickName());
            dto.setList(list2);
            list1.add(dto);
        }
        Page<UserScoreDto> pages = new Page<>();
        pages.setPages(page.getPages());
        pages.setRecords(list1);
        pages.setSize(page.getSize());
        pages.setCurrent(page.getCurrent());
        pages.setTotal(page.getTotal());
        return pages;
    }
}
